package com.example.redis.demo.configure;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfigure {


    @Autowired
    private RabbitProperties rabbitProperties;


    @Bean
    public RabbitTemplate rabbitTemplate() {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate();
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(this.rabbitProperties.getHost(),
                this.rabbitProperties.getPort());
        cachingConnectionFactory.setPassword(this.rabbitProperties.getPassword());
        cachingConnectionFactory.setUsername(this.rabbitProperties.getUsername());
        rabbitTemplate.setConnectionFactory(cachingConnectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //是生产者发布之后消费者确认之后的回调
        System.out.println("rabbit properties=" + this.rabbitProperties.getPublisherConfirmType());
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                String messageId = correlationData.getId();
                boolean ackRes = ack;
                System.out.println("收到消息id=" + messageId + ",ack=" + ackRes + ",失败原因=" + cause);
            }
        });
        rabbitTemplate.setMessagePropertiesConverter(new DefaultMessagePropertiesConverter());
        return rabbitTemplate;
    }

    @Bean(name = "user")
    public TopicExchange userExchange() {
        return new TopicExchange("user", true, false);
    }

    @Bean(name = "login")
    public Queue loginQueue() {
        return new Queue("login", true, false, false);
    }


    @Bean(name = "sign")
    public Queue signQueue() {
        return new Queue("sign", true, false, false);
    }


    @Bean(name = "loginBinding")
    public Binding bingLoginToX(@Qualifier("login") Queue loginQueue,
                                @Qualifier("user") TopicExchange userExchange) {
        return BindingBuilder.bind(loginQueue).to(userExchange).with("login.*");
    }


    @Bean(name = "signBinding")
    public Binding bingSignToX(@Qualifier("sign") Queue signQueue,
                               @Qualifier("user") TopicExchange userExchange) {
        return BindingBuilder.bind(signQueue).to(userExchange).with("sign.#");
    }

}
